import { openAppClient, showLoading, hideLoading } from '@/utils/common'
import equipPlan from '@/api/equip/common.js'
import equipHandover from '@/api/equip/handoverAudit.js'
import dailyManagement from '@/api/equip/dailyManagementAudit.js'
// 审核相关操作
/**
 * moduleType
 * 0 设备计划
 * 1 设备交接
 * 2 日常管理
 *
 */
function exas (arg, vueObject, requestFn, moduleType) {
    const _this = vueObject
    //判断是那个模块
    switch(moduleType) {
    case 0:
        _this.request = equipPlan
        break
    case 1 :
        _this.request = equipHandover
        break
    case 2 :
        _this.request = dailyManagement
        break
    }
    if (arg === 'audit') {
        showLoading()
        _this.errorList = []
        Promise.all(requestFn())
            .then(() => {
                hideLoading()
                _this.setAuditParams(_this.auditParams)
                // 客户端弹窗弹出本地页面
                openAppClient('/exaDialog', '填写审核意见', () => {
                    const auditParams = JSON.parse(localStorage.vuex).auditParams
                    showLoading()
                    _this.request.audit(auditParams).then(() => {
                        hideLoading()
                        _this.clientPop('suc', '审核成功', _this.handleClose)
                    }).catch(()=>{
                        hideLoading()
                    })

                }, null, null, null, 'small')
            })
            .catch(err => {
                console.log('errMsg：', err)
                _this.scorllError()
            })
            .finally(() => {
                hideLoading()
            })
    } else if (arg === 'undoAudit') {
        _this.clientPop('info', _this.undoAudit, ()=>{
            showLoading()
            _this.request.undoworkt(_this.auditParams).then(() => {
                hideLoading()
                _this.clientPop('suc', '撤回成功', _this.handleClose)
            }).finally(() => {
                hideLoading()
            })
        })
    } else if (arg === 'submitCommit') {
        _this.submitCommit()  //保存并提交
    } else if (arg === 'submit') {
        _this.saveAll()   //保存
    } else if (arg === 'nullify') {
        _this.setAuditParams(_this.nullifyParams)
        // 客户端弹窗弹出本地页面
        openAppClient('/nullifyDialog', '单据作废原因', () => {
            const auditParams = JSON.parse(localStorage.vuex).auditParams
            showLoading()
            _this.request.nullify(auditParams).then(() => {
                hideLoading()
                _this.clientPop('suc', '作废成功', _this.handleClose)
            }).finally(() => {
                hideLoading()
            })
        })
    } else if (arg === 'deleteData') {
        _this.deleteBills()
    }
}
//保存全部数据
function saveAll (vueObject, requestFn) {
    const _this = vueObject
    showLoading()
    _this.errorList = []
    Promise.all(requestFn())
        .then(() => {
            hideLoading()
            _this.init()
            _this.clientPop('suc', '保存成功')
        })
        .catch(err => {
            let errMsg = null
            if(  typeof err === 'string') {
                errMsg = err
            }else if(err instanceof Array) {
                errMsg = err.join(',')
            }else if(err instanceof Object) {
                if(err.code) {
                    errMsg = Object.values(err.data)
                }else{
                    errMsg = err
                }
            }
            _this.clientPop('err', `保存失败${errMsg !== null ? '\n错误信息：' +  errMsg : ''}
                    `, ()=>{
                _this.scorllError()
            })
        })
        .finally(() => {
            hideLoading()
        })
}
//保存并提交
function submitCommit (vueObject, requestFn, moduleType) {
    const _this = vueObject
    switch(moduleType) {
    case 0:
        _this.request = equipPlan
        break
    }
    showLoading()
    _this.errorList = []
    Promise.all(requestFn())
        .then(() => {
            _this.request.commit(_this.auditParams).then(()=>{
                hideLoading()
                _this.clientPop('suc', '提交成功', _this.handleClose)
            })
        })
        .catch(err => {
            let errMsg = null
            if(  typeof err === 'string') {
                errMsg = err
            }else if(err instanceof Array) {
                errMsg = err.join(',')
            }else if(err instanceof Object) {
                if(err.code) {
                    errMsg = Object.values(err.data)
                }else{
                    errMsg = err
                }
            }
            _this.clientPop('err', `提交失败${errMsg !== null ? '\n错误信息：' +  errMsg : ''}
                    `, ()=>{
                _this.scorllError()
            })
        })
        .finally(() => {
            hideLoading()
        })
}
export {
    exas,
    saveAll,
    submitCommit,
}